/**
 * Created by yangxianming on 2020/12/30.
 */

var devloper = ["","薛浩","唐良瀛","张开强","姜进","产品"];
var progressList = ["", "福员外", "口福"];
var globState = ["全部","打开","解决","关闭","偶发"];
//
//接口定义 都是以api开头 所需参数都通过方法传入
//function apiGetCorpList(dofun, envText){}
//
//更新bug
function apiUpdateBug(uuid, describe, state){
    if(describe == "" && state ==""){
        return
    }
    //获取数据
    var saveData = {"action": "update"};
    var data = {};
    data["uuid"] = uuid;
    if(describe != ""){
        data["describe"] = describe
    }
    if(state != ""){
        data["state"] = state
    }
    saveData["data"] = data;

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/progressBug/saveProgressBug", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    //drawBugList(r["result"])
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(saveData));
}

//获取bug列表
function apiShowBug(){
    //获取数据
    var saveData = {"action": "showBug"};
    saveData["data"] = {
        "progressname":selectedText(document.getElementById("version")),
        "createtime":document.getElementById("year").value,
        "project":selectedText(document.getElementById("project")),
        "owner":selectedText(document.getElementById("owner")),
        "state":selectedValue(document.getElementById("state")),
        "key":document.getElementById("key").value
    };
    //console.log(saveData);
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/progressBug/showProgressBug", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    drawBugList(r["result"])
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(saveData));
}

//提交问题
function apiSaveBug(){
    //获取数据
    var saveData = {"action": "addBug"};
    var data = {
        "id":document.getElementById("bugId").innerText,
        "progressname":selectedText(document.getElementById("version1")),
        "module":selectedText(document.getElementById("module1")),
        "owner":selectedText(document.getElementById("developer")),
        "first":selectedValue(document.getElementById("first")),
        "title":document.getElementById("title").value,
        "describe":document.getElementById("describe").value
    };
    var pa = document.getElementById("img").getElementsByTagName("img");
    var picarr = [];
    if(pa.length != 0){
        for(var i = 0; i < pa.length; i++){
            var v = pa[i].getAttribute("src");
            picarr.push(v);
        }
    }
    data["picarr"] = picarr;
    saveData["data"] = data;

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/progressBug/saveProgressBug", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    controlTopDiv('close', '');
                    apiShowBug();
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(saveData));
}

//获得模块列表
function apiGetModule(project, needSelected, obj){
    //var project = selectedText(document.getElementById("project1"));
    if(project == ""){
        return;
    }
    var value = "action=getModule&project="+project;
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/progressBug/getModule?" + value, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    loadSelectList(document.getElementById("module1"), r["result"]);
                    if(needSelected == "1"){
                        //这里加载列表的选中值
                        setSelectChecked(document.getElementById("module1"), obj.getAttribute("module"));
                    }
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}

//获得版本列表
function apiGetVersion(selectVersion){
    var year = document.getElementById("year").value;
    var project = selectedText(document.getElementById("project"));
    var value = "action=getVersion&year="+year+"&project="+project;
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/progressBug/getVersion?" + value, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    loadSelectList(document.getElementById("version"), r["result"]);
                    if(selectVersion != "undefined"){
                        var version = document.getElementById("version");
                        setSelectChecked(version, selectVersion);
                        //showCase();
                    }
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}

function apiUploadImg(){
    var a1 = document.getElementById("a1");
    var img = document.getElementById("img");
    var fi = document.getElementById('file').files[0];
    var action = "action=bugImage";
    var form = new FormData();
    form.append("file", fi);

    if(fi == undefined){
        return;
    }

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    var divImg = document.createElement("div");
                    divImg.style.width = "100%";
                    var newImg = document.createElement("img");
                    newImg.setAttribute("style", "max-width: 600px; max-height: 400px");
                    newImg.setAttribute("src", r["result"]);
                    newImg.title = "双击删除";
                    newImg.ondblclick = function(){drawDeletePic(divImg)};
                    divImg.appendChild(newImg);
                    img.appendChild(divImg);
                    fi.innerText = "";
                    document.getElementById('file').value = "";
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("POST", "../uploadFile?action=bugImage", true);
    xmlHttp1.send(form);
}


//
//页面渲染 都是以draw开头
//function drawGetCorpList(data){}
//
//改变bug状态
function drawBugState(obj){
    var uuid = obj.parentNode.parentNode.getAttribute("id");
    var state = obj.innerText;
    if(state == "打开"){
        obj.innerText = globState[2];
        apiUpdateBug(uuid, "", "2");
        return;
    }
    if(state == "解决"){
        obj.innerText = globState[3];
        apiUpdateBug(uuid, "", "3");
        return;
    }
    if(state == "关闭"){
        obj.innerText = globState[4];
        apiUpdateBug(uuid, "", "4");
        return;
    }
    if(state == "偶发"){
        obj.innerText = globState[1];
        apiUpdateBug(uuid, "", "1");
        return;
    }
}

//绘制bug列表
function drawBugList(data){
    var table = document.getElementById("table");
    var tableheader = document.getElementById("tableheader");
    table.innerText = "";
    table.appendChild(tableheader);
    for(var i=0; i<data.length; i++){
        //每条数据的数据结构
        //["6","dLJqZAKwt2AJfmZD","迭代版本","模块","标题","内容","3(优先级)",
        //"[\"../bugimage/1614061212769kremaR\",\"../xx/xx\"]","1","张开强",	"2021-02-23 14:20:17.0"],
        var tr = document.createElement("tr");
        tr.onmouseover = function(){this.style.backgroundColor = '#ffcfce'};
        tr.onmouseout = function(){this.style.backgroundColor = 'transparent'};
        tr.setAttribute("id", data[i][1]);
        tr.setAttribute("version", data[i][2]);
        tr.setAttribute("module", data[i][3]);
        tr.setAttribute("bugTitle", data[i][4]);
        tr.setAttribute("bugContent", data[i][5]);

        var td1 = document.createElement("td");
        td1.innerText = (i+1).toString();
        tr.appendChild(td1);

        var td1_1 = document.createElement("td");
        var first = data[i][6];
        if(first == "1"){
            td1_1.innerText = "阻碍";
            td1_1.style.color = "#c7043f";
            td1_1.style.fontWeight = "bold";
        }
        if(first == "2"){
            td1_1.innerText = "严重";
            td1_1.style.color = "#c707af";
            td1_1.style.fontWeight = "bold";
        }
        if(first == "3") td1_1.innerText = "一般";
        tr.appendChild(td1_1);

        var td2 = document.createElement("td");
        td2.innerText = data[i][9];
        tr.appendChild(td2);

        var td3 = document.createElement("td");
        var pageDescribe = "";
        var title = data[i][4].trim();
        var describe = data[i][5];
        if(title == ""){
            pageDescribe = describe.substr(0, describe.indexOf("\n"));
        }else{
            pageDescribe = title;
        }
        td3.innerText = pageDescribe;
        tr.appendChild(td3);

        var td4 = document.createElement("td");
        var span1 = document.createElement("span");
        span1.className = "forTD";
        var state = data[i][8];
        if(state == "1") span1.innerText = globState[1];
        if(state == "2") span1.innerText = globState[2];
        if(state == "3") span1.innerText = globState[3];
        if(state == "4") span1.innerText = globState[4];
        span1.onclick = function(){drawBugState(this)};

        //操作控件
        td4.appendChild(span1);
        var span2 = document.createElement("span");
        var pic = data[i][7];
        span2.className = "forTD";
        span2.setAttribute("imgAddr", pic);
        span2.onclick = function(){controlPicTitle("open", this)};
        span2.innerText = "图片";
        td4.appendChild(span2);
        var span3 = document.createElement("span");
        span3.className = "forTD";
        span3.onclick = function(){controlTopDivBut("edit", this.parentNode.parentNode.getAttribute("id"))};
        span3.innerText = "编辑";
        td4.appendChild(span3);
        tr.appendChild(td4);



        table.appendChild(tr);
    }
}

//预览进度表
function drawDeletePic(obj){
    if (confirm("确定要移除图片嘛？")==true){
        var img = document.getElementById("img");
        obj.remove(obj);
        //img.remove(obj.parent);
    }
}



//
//页面调用 数据获取，接口层与渲染层的连接
//
function initializeData(){
    //通过url拿到项目参数
    var pro = decodeURI(getQueryVariable("project"));

    //查询历史档案页面 填充当前年份
    var da = new Date();
    document.getElementById("year").value = da.getFullYear();

    //查询历史档案页面 填充项目下拉
    loadSelectList(document.getElementById("project"), progressList);

    //加载迭代版本
    apiGetVersion(pro);

    //加载开发人员名称
    loadSelectList(document.getElementById("developer"), devloper);
    loadSelectList(document.getElementById("owner"), devloper);

    //加载状态选项state
    loadSelectList(document.getElementById("state"), globState);

    //延迟加载列表
    setTimeout(
        function(){apiShowBug()},
        500
    )
}



//图片展示弹框
var imgArr = [];
var picIndex = -1;
function controlPicTitle(mySwitch, spanObj){
    var topDiv = document.getElementById('pic');
    var lrButton = document.getElementsByClassName("butPic");

    if (mySwitch == "open") {
        imgArr = JSON.parse(spanObj.getAttribute("imgAddr"));
        if(imgArr.length == 0) return;
        if(imgArr.length == 1){
            lrButton[0].style.display = "none";
            lrButton[1].style.display = "none";
        }
        picIndex = 0;
        var img = document.getElementById("showPic");
        img.innerText = "";
        var newImg = document.createElement("img");
        newImg.setAttribute("style", "max-width: 1300px; max-height: 900px");
        newImg.setAttribute("src", imgArr[0]);
        img.appendChild(newImg);
        ////显示页面
        //topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
    }
    if (mySwitch == "close"){
        topDiv.style.display = "none";
        lrButton[0].style.display = "block";
        lrButton[1].style.display = "block";
        picIndex = -1;
        //document.getElementById("module1").innerText = "";
    }
}
//图片切换
function switchPic(control){
    if(control == "-"){
        if(picIndex == 0) return;
        picIndex--;
    }
    if(control == "+"){
        if(picIndex == imgArr.length-1) return;
        picIndex++;
    }
    var img = document.getElementsByTagName("img")[0];
    img.setAttribute("src", imgArr[picIndex]);
}

//隐藏弹窗控制方法
function controlTopDivBut(mySwitch, id){
    var topDiv = document.getElementById('bug');
    var versionText = selectedText(document.getElementById("version"));
    if(mySwitch == "edit"){
        var tr = document.getElementById(id);
        var selectedVersion = tr.getAttribute("version");
        var version1 = document.getElementById("version");
        document.getElementById("version1").innerHTML = version1.innerHTML;
        //设置版本选中值
        setSelectChecked(document.getElementById("version1"), selectedVersion);
        //加载模块,设置模块值
        apiGetModule(selectedVersion, "1", tr);
        //加载归属人
        setSelectChecked(document.getElementById("developer"), tr.getElementsByTagName("td")[2].innerText);
        //加载优先级 first
        setSelectChecked(document.getElementById("first"), tr.getElementsByTagName("td")[1].innerText);
        //清空加载的数据
        document.getElementById("img").innerText = "";
        document.getElementById("title").value = "";
        document.getElementById("describe").value = "";
        //加载标题,内容
        document.getElementById("title").value = tr.getAttribute("bugTitle");
        document.getElementById("describe").value = tr.getAttribute("bugContent");
        //加载bugid
        document.getElementById("bugId").innerText = tr.getAttribute("id");
        //加载图片
        var img = tr.getElementsByTagName("td")[4].getElementsByTagName("span")[1];
        var imgArr = JSON.parse(img.getAttribute("imgaddr"));
        for(var i = 0; i < imgArr.length; i++){
            var imgDiv = document.createElement("div");
            imgDiv.setAttribute("style", "width: 100%");
            var imginner = document.createElement("img");
            imginner.setAttribute("style", "max-width: 600px; max-height: 400px");
            imginner.setAttribute("src", imgArr[i]);
            imginner.setAttribute("title", "双击删除");
            imginner.ondblclick = function(){drawDeletePic(this.parentNode)};
            imgDiv.appendChild(imginner);
            document.getElementById("img").appendChild(imgDiv);
        }


        //显示页面
        topDiv.style.display = "block";
    }
    if (mySwitch == "open") {
        //加载本版
        var version = document.getElementById("version");
        document.getElementById("version1").innerHTML = version.innerHTML;
        //设置选中值
        setSelectChecked(document.getElementById("version1"), versionText);
        //加载模块
        apiGetModule(versionText);
        //清空加载的数据
        document.getElementById("img").innerText = "";
        document.getElementById("title").value = "";
        document.getElementById("describe").value = "";
        document.getElementById("bugId").innerText = "";

        //显示页面
        //topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
    }
    if (mySwitch == "close"){
        topDiv.style.display = "none";
        document.getElementById("module1").innerText = "";
    }
}